import CryptoJS from 'crypto-js'
export function isWeChatBrowser() {
	var userAgent = window.navigator.userAgent.toLowerCase()
	return userAgent.match(/micromessenger/i) !== null
}

function randomString() {
	let result = ''
	const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
	const charactersLength = characters.length
	for (let i = 0; i < 32; i++) {
		result += characters.charAt(Math.floor(Math.random() * charactersLength))
	}
	return result
}

export function signfn(url, appKey = 'iosb83d14cd79b4098f3da9b4933ccdd', key =
	'ios11ee86c3fdc3d14cd9a48f3dgjkad') {
	const authTs = new Date().getTime()
	const authOnce = randomString()
	const authSign = CryptoJS.MD5(`act=${url}&app_key=${appKey}&nonce=${authOnce}&ts=${authTs}&key=${key}`)
		.toString()
	return {
		'auth-ts': authTs,
		'auth-nonce': authOnce,
		'auth-sign': authSign,
		'auth-key': appKey,
		'auth-type-id': 1,
	}
}
// 判断是否为空
export function isNotEmpty(value) {
	return value !== null && value !== undefined && value !== '';
}
// 过滤掉空属性
export function filterEmptyProperties(obj) {
	const filteredObj = {};
	Object.keys(obj).forEach(key => {
		if (isNotEmpty(obj[key])) {
			filteredObj[key] = obj[key];
		}
	});
	return filteredObj;
}
//获取url参数
export function getURLParams(url) {
	const params = {};
	const urlParts = url.split('?');

	if (urlParts.length > 1) {
		const queryString = urlParts[1];
		const keyValuePairs = queryString.split('&');
		keyValuePairs.forEach(pair => {
			const keyValue = pair.split('=');
			const key = decodeURIComponent(keyValue[0]);
			const value = decodeURIComponent(keyValue[1] || '');
			params[key] = value;
		});
	}

	return params;
}

// 格式化金额
export function splitNumber(value) {
	// 将数值转换为字符串，并按小数点分割
	const parts = String(value).split('.');

	// 整数部分
	const integerValue = parts[0] || '0';

	// 小数部分
	const decimalValue = parts[1] ? parts[1].padEnd(2, '0').slice(0, 2) : '00';

	// 返回整数部分和小数部分
	return {
		integerValue,
		decimalValue
	};
}